package cn.edu.qfnu.oj.web.controller;


import cn.edu.qfnu.oj.entity.Problem;
import cn.edu.qfnu.oj.service.ProblemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "problem/")
public class ProblemController {

    private static final Logger log = LoggerFactory.getLogger(ProblemController.class);

    private final ProblemService problemService;

    @Autowired
    public ProblemController(ProblemService problemService) {
        this.problemService = problemService;
    }

    @RequestMapping(value = "index1", method = RequestMethod.GET)
    public ModelAndView create() {
        return new ModelAndView("createProblem");
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> create(String title, String content,
                                      String inputDesc, String outputDesc, String sampleInput, String sampleOutput,
                                      String classify, String difficulty, String hint, String sources,
                                      Integer memoryLimit, Integer timeLimit, String isAcm, String isTemp) {

        Map<String, String> jsonMap = new HashMap<>();
        try {
            Problem problem = problemService.createProblem(title, content, inputDesc, outputDesc,
                    sampleInput, sampleOutput, classify, LocalDateTime.now(), difficulty,
                    hint, sources, memoryLimit, timeLimit, isAcm, isTemp);
            problemService.saveOrUpdate(problem);
            jsonMap.put("result", "success");
            log.info("创建了一个新题目: " + problem.getTitle());
        } catch (Exception e) {
            jsonMap.put("result", "error");
            log.error("创建题目时出错", e);
        }
        return jsonMap;
    }


    @RequestMapping(value = "mo", method = RequestMethod.GET)
    public String modify() {
        return "moProblem";
    }

    @RequestMapping(value = "moProblem", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> modify(Integer id, String title, String content,
                                      String inputDesc, String outputDesc, String sampleInput, String sampleOutput,
                                      String classify, String difficulty, String hint, String sources,
                                      Integer memoryLimit, Integer timeLimit, String isAcm, String isTemp) {
        Map<String, String> jsonMap = new HashMap<>();

        //依次传入问题id 标题， 内容， 输入描述，输出描述，输入样例，输出样例，
        // 修改时间，类型，难度，暗示，来源，内存限制，时间限制，是否是acm，是否是临时题目
        try {
            Problem problem =
                    problemService.modify(problemService.findById(id), title, content, inputDesc, outputDesc,
                    sampleInput, sampleOutput, LocalDateTime.now(),
                    classify, difficulty, hint, sources,
                    memoryLimit, timeLimit, isAcm, isTemp);

            problemService.merge(problem);
            jsonMap.put("result", "success");
            log.info("修改了题目：" + problem.getId() + " " + problem.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("result", "failed");
            log.error("修改题目时出错", e);
        }
        return jsonMap;
    }

    @RequestMapping(value = "index3", method = RequestMethod.GET)
    public String delete() {
        return "deleteProblem";
    }

    @RequestMapping(value = "deleteProblem", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(Integer id) {
        Map<String, String> jsonMap = new HashMap<>();

        try {
            problemService.removeById(id);
            jsonMap.put("result", "success");
            log.info("删除了题目" + id);
        } catch (Exception e) {
            jsonMap.put("result", "failed");
            log.error("删除题目时出错");
        }

        return jsonMap;
    }

}
